// ServerConfiguration.cpp: implementation of the ServerConfiguration class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "ServerConfiguration.h"

#include "CommonLib\Registry.h"
#include "CommonLib\ReportExporter.h"
#include "CommonLib\ValueConverter.h"
#include "CommonLib\FileHelper.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ServerConfiguration::ServerConfiguration()
{
}

ServerConfiguration::~ServerConfiguration()
{
}

ServerConfiguration * ServerConfiguration::m_pInstance = NULL;

ServerConfiguration * ServerConfiguration::Instance()
{
	if (m_pInstance == NULL)
	{
		m_pInstance = new ServerConfiguration;
	}

	return m_pInstance;
}

void ServerConfiguration::DeleteInstance()
{
	if (m_pInstance) delete m_pInstance;

	m_pInstance = NULL;
}

void ServerConfiguration::Init()
{
	m_strRegistryEntry = "SOFTWARE\\Eflong\\KWSystem\\1.0\\";
	m_strAppPath=CFileHelper::Instance()->GetAppPath().GetBuffer(0);

	Registry reg(m_strRegistryEntry);
	m_strWorkDir = reg.Read("Server", "WorkDirectory", m_strAppPath);
	m_nPort = reg.Read("Server", "Port", 7000);
	m_strMACAddress = reg.Read("Server", "MACAddress", "");
	m_strLicenseKey = reg.Read("Server", "LicenseKey", "");

	m_bStoreExcessRecordToDB = reg.Read("Server", "StoreExcessRecordToDB", 1);
	m_dwIntervalOfStoringExcessRecord = reg.Read("Server", "IntervalOfStoringExcessRecord", 1000);

	m_bAutoReport = reg.Read("Server", "AutoReport", 1);
	m_bAutoCoalMineReport = reg.Read("Server", "AutoCoalMineReport", 1);
	m_nAutoReportTime = reg.Read("Server", "AutoReportTime", 800);
	m_nAutoReportExportFormat = reg.Read("Server", "AutoReportExportFormat", ReportExporter::ExcelWorkbookFormat);
	m_fCH4Limit = atof(reg.Read("Server", "CH4Limit", "0").c_str());
	ValueConverter::StringTokenizer( reg.Read("Server", "ExcessReportTypes", "1, 4").c_str(), ",", m_vecExcessReportTypes);
	m_dwAutoReportStates = reg.Read("Server", "AutoReportStates", 3);
	ValueConverter::StringTokenizer(reg.Read("Server", "DigitalReportTypes", "13, 19").c_str(), ",", m_vecDigitalReportTypes);
	m_bAutoAnalogReport = reg.Read("Server", "AutoAnalogReport", 1);
}

void ServerConfiguration::Cleanup()
{
	Save();
}

void ServerConfiguration::Save()
{
	Registry reg(m_strRegistryEntry);

	reg.Write("Server", "MACAddress", m_strMACAddress);
	reg.Write("Server", "LicenseKey", m_strLicenseKey);

	reg.Write("Server", "StoreExcessRecordToDB", m_bStoreExcessRecordToDB);
	reg.Write("Server", "IntervalOfStoringExcessRecord", m_dwIntervalOfStoringExcessRecord);

	reg.Write("Server", "AutoReport", m_bAutoReport);
	reg.Write("Server", "AutoCoalMineReport", m_bAutoCoalMineReport);
	reg.Write("Server", "AutoReportTime", m_nAutoReportTime);
	reg.Write("Server", "AutoReportExportFormat", m_nAutoReportExportFormat);
	CString str;
	str.Format("%.2f", m_fCH4Limit);
	reg.Write("Server", "CH4Limit", std::string(str));
	str = "";
	if (m_vecExcessReportTypes.size() > 0)
	{
		str.Format("%d", m_vecExcessReportTypes[0]);
		for (int i = 1;i < m_vecExcessReportTypes.size();i++)
		{
			CString strTemp;
			strTemp.Format(",%d", m_vecExcessReportTypes[i]);
			str += strTemp;
		}
	}
	reg.Write("Server", "ExcessReportTypes", std::string(str));
	reg.Write("Server", "AutoReportStates", m_dwAutoReportStates);
	str = "";
	if (m_vecDigitalReportTypes.size() > 0)
	{
		str.Format("%d", m_vecDigitalReportTypes[0]);
		for (int i = 1;i < m_vecDigitalReportTypes.size();i++)
		{
			CString strTemp;
			strTemp.Format(",%d", m_vecDigitalReportTypes[i]);
			str += strTemp;
		}
	}
	reg.Write("Server", "DigitalReportTypes", std::string(str));
	reg.Write("Server", "AutoAnalogReport", m_bAutoAnalogReport);
}